﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlTypes;
using System.Data;
using DataAccessLayer;

namespace BusinessLayer
{
    public class carts
    {
       public Int32 shopping_cart_id {get;set;}
       public string user_id {get;set;} 
       public string equipment_id {get;set;} 
       public string  equipment_name {get;set;}
       public decimal price {get;set;} 
       public string  equipment_image {get;set;}
       public decimal subtotal { get; set; }

        public carts()
        {
            shopping_cart_id = 0;
            user_id = "";
            equipment_id = "";
            equipment_name = "";
            price = 0;
            equipment_image = "";
            subtotal = 0;
 
        }
        public carts(Int32 s_c_i,string u_i,string eq_d,string eq_i,string eq_n,decimal p,string eq_im,decimal su)
        {
            shopping_cart_id = s_c_i;
            user_id = u_i;
            equipment_id = eq_d;
            equipment_name = eq_n;
            price = p;
            equipment_image = eq_im;
            subtotal = su;
        }

        public void save_carts()
        {
            DataAccessLayer.csDAL objal = new DataAccessLayer.csDAL();
            List<csparameterListType> par = new List<csparameterListType>();

            par.Add(new csparameterListType("@SHOPPING_CART_ID", System.Data.SqlDbType.Int, shopping_cart_id));
            par.Add(new csparameterListType("@USER_ID", System.Data.SqlDbType.Decimal, user_id  ));
            par.Add(new csparameterListType("@EQUIPMENT_ID", System.Data.SqlDbType.VarChar, equipment_id));
            par.Add(new csparameterListType("@EQUIPMENT_NAME", System.Data.SqlDbType.VarChar, equipment_name ));
            par.Add(new csparameterListType("@PRICE", System.Data.SqlDbType.Decimal , price));
            par.Add(new csparameterListType("@EQUIPMENT_IMAGE", System.Data.SqlDbType.Int , equipment_image ));
            par.Add(new csparameterListType("@SUBTOTAL", System.Data.SqlDbType.Decimal, subtotal));

            objal.executespreturndr("SAVE_CARTS", par);
        }
             
        public List<carts> get_carts()
        {
            List<csparameterListType> par = new List<csparameterListType>();
            List<carts> objlist = new List<carts>();
            DataAccessLayer.csDAL objdal = new DataAccessLayer.csDAL();
            IDataReader datareader = null;

            datareader = objdal.executespreturndr("GET_CARTS");
            while (datareader.Read())
            {
                carts  objP = new carts ();
                populate_reader(datareader, objP);
                objlist.Add(objP);
            }
            return objlist;
        }

        private void populate_reader(IDataReader datareader, carts  objP)
        {
            objP.shopping_cart_id = datareader.GetInt32(0);
            objP.price = datareader.GetDecimal(2);
            objP.subtotal = datareader.GetDecimal(3);
            objP.equipment_id = datareader.GetString(4);
            objP.equipment_image = datareader.GetString(5);
            objP.equipment_name = datareader.GetString(6);
            objP.user_id = datareader.GetString(7);
        }

    }
}